#include "HelloWorldScene.h"
#include "AudioEngine.h"
USING_NS_CC;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    auto closeItem = MenuItemImage::create( "CloseNormal.png",
                                           "CloseSelected.png",
                                           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
    
	closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                                origin.y + closeItem->getContentSize().height/2));
    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu, 1);
    
    auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);
    label->setPosition(Vec2(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - label->getContentSize().height));
    this->addChild(label, 1);

    auto sprite = Sprite::create("HelloWorld.png");
    sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
    this->addChild(sprite, 0);
   

	CreateMenu();

    return true;
}
void HelloWorld::onExit()
{
	Layer::onExit();
}

void HelloWorld::menuCloseCallback(Ref* pSender)
{
    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

void HelloWorld::CreateMenu()
{
	Size visibleSize = Director::getInstance()->getVisibleSize();
	Vec2 origin = Director::getInstance()->getVisibleOrigin();

	float y_offset = 80.0f;

	auto closeItem = MenuItemImage::create("CloseNormal.png","CloseSelected.png",
		CC_CALLBACK_1(HelloWorld::onLeftCallback, this));
	closeItem->setPosition(Vec2(origin.x + visibleSize.width * 0.25f ,
		origin.y + visibleSize.height * 0.5f + 80 + y_offset));
	auto openItem = MenuItemImage::create("CloseNormal.png", "CloseSelected.png",
		CC_CALLBACK_1(HelloWorld::onRightCallback, this));
	openItem->setPosition(Vec2(origin.x + visibleSize.width * 0.75f,
		origin.y + visibleSize.height * 0.5f + 80 + y_offset));

	auto vv1 = MenuItemImage::create("CloseNormal.png", "CloseSelected.png",
		CC_CALLBACK_1(HelloWorld::onLeftCallback1, this));
	vv1->setPosition(Vec2(origin.x + visibleSize.width * 0.25f,
		origin.y + visibleSize.height * 0.5f + y_offset));
	auto vv2 = MenuItemImage::create("CloseNormal.png", "CloseSelected.png",
		CC_CALLBACK_1(HelloWorld::onRightCallback1, this));
	vv2->setPosition(Vec2(origin.x + visibleSize.width * 0.75f,
		origin.y + visibleSize.height * 0.5f + y_offset));


	auto menu = Menu::create(closeItem, openItem, vv1, 
		vv2,NULL);
	menu->setPosition(Vec2::ZERO);
	this->addChild(menu, 1);
}

void HelloWorld::onLeftCallback(cocos2d::Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	m_Loop1AudioId = experimental::AudioEngine::play2d("sound/loop1.m4a",true);
#else
	m_Loop1AudioId = experimental::AudioEngine::play2d("sound/loop1.ogg", true);
#endif	
}

void HelloWorld::onRightCallback(cocos2d::Ref* pSender)
{
	if (m_Loop1AudioId != experimental::AudioEngine::INVALID_AUDIO_ID)
	{
		experimental::AudioEngine::stop(m_Loop1AudioId);
		m_Loop1AudioId = experimental::AudioEngine::INVALID_AUDIO_ID;
	}	
}

void HelloWorld::onLeftCallback1(cocos2d::Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	m_Loop2AudioId = experimental::AudioEngine::play2d("sound/loop2.m4a", true);
#else
	m_Loop2AudioId = experimental::AudioEngine::play2d("sound/loop2.ogg", true);
#endif
}
void HelloWorld::onRightCallback1(cocos2d::Ref* pSender)
{
	if (m_Loop2AudioId != experimental::AudioEngine::INVALID_AUDIO_ID)
	{
		experimental::AudioEngine::stop(m_Loop2AudioId);
		m_Loop2AudioId = experimental::AudioEngine::INVALID_AUDIO_ID;
	}
}


